import {
  agentNews,
  readdNews

} from '~/services/api/message'
import {
  guid
} from '~/utils/util'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsListLoadStatus: 0,
    pageLoading: false,
    newsList: [],
    msgType: '',
    noMore: false,
    image: 'https://tdesign.gtimg.com/miniprogram/images/empty1.png',
    pullDownRefreshing: false,
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
  },
  goodListPagination: {
    pageNo: 1,
    pageSize: 10,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var options = currentPage.options //如果要获取url中所带的参数可以查看options 
    this.initNews(options.type);
    this.setData({
      msgType: options.type
    })
  },

  async initNews(type) {
    let {
      code,
      data
    } = await agentNews({
      // "endTime": "string",
      "pageNo": 1,
      "pageSize": 10,
      "param": {
        "msgType": type
      },
      // "startTime": "string",
      // "timeRange": "string"
    });

    this.setData({
      newsList: data.entities,
    })
  },

  async readFn(e) {
    const role = wx.getStorageSync('role');
    const userId = role?.userId;
    let item = e.currentTarget.dataset.group;
    let {
      data,
      code
    } = await readdNews({
      ackUserId: userId,
      msgKid: item.msgKid
    })
    console.log(e.currentTarget.dataset.group);
    let index = this.data.newsList.findIndex((el) => {
      return el.msgKid == item.msgKid
    })
    if (data) {
      this.data.newsList.splice(index, 1, {
        ...this.data.newsList[index],
        ackFlag: 1
      })
      this.setData({
        newsList: this.data.newsList
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    this.loadGoodsList(true);
  },
  onPullDownRefresh() {
    this.loadHomePage();
  },
  onReTry() {
    this.loadGoodsList();
  },
  async loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
      this.setData({
        pageLoading: false,
      });
    }
    this.setData({
      goodsListLoadStatus: 1
    });
    const pageSize = this.goodListPagination.pageSize;
    let pageIndex = this.goodListPagination.pageNo + 1;
    if (fresh) {
      pageIndex = 1;
    }
    try {
      const {
        data
      } = await agentNews({
        "pageNo": pageIndex,
        "pageSize": pageSize,
        "param": {
          "msgType": this.data.msgType
        },
      });
      this.setData({
        newsList: fresh ? data.entities : this.data.newsList.concat(data.entities),
        goodsListLoadStatus: data.entities.length > 1 ? 0 : 2,
      });
      this.goodListPagination.pageSize = data.pageSize;
      this.goodListPagination.pageNo = data.pageNo;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})